#pragma once

#include <iostream>
#include <string>
#include <functional>
// #include "listener.hpp"

#include "InetAddr.hpp"
class listener;
class reactor;
class connection;

using connection_t = std::shared_ptr<connection>;
using func_t = std::function<void(connection *)>;

// 普通的 fd, listensockfd;
// 让对fd的处理方式采用用一种
// 描述一个连接
class connection
{
public:
    connection() : _sockfd(-1), _events(0)
    {
    }
    void setSockfd(int sockfd) { _sockfd = sockfd; }
    ~connection()
    {
    }
    int sockfd() { return _sockfd; }
    void setEvents(uint32_t events) { _events = events; }
    uint32_t getEvents() { return _events; }
    void setOwner(reactor* owner) { _owner = owner;}
    reactor* getOwner() { return _owner;}
    void setPeerInfo(const InetAddr &peer_addr) { _peer_addr = peer_addr; }
    void append(const std::string& in){ _inbuffer += in;} // 把收到的数据全部添加到自己的接收缓冲区
    std::string outString() { return _outBuffer;} // for test
    std::string &inString() { return _inbuffer;}
    virtual void recever() = 0;
    virtual void sender() = 0;
    virtual void excepter() = 0;

protected:
    int _sockfd;
    std::string _inbuffer;
    std::string _outBuffer;

    InetAddr _peer_addr; // 对应的那个客户端

    // 我关心的事件
    uint32_t _events;


    // 添加一个 指针
    reactor *_owner;
};

// class factory
// {
// public:
//     static std::shared_ptr<connection> BuildConnection(int fd, uint32_t events)
//     {
//         std::shared_ptr<connection> conn = std::make_shared<connection>();
//         conn->setSockfd(fd);
//         conn->setEvents(events);
//         // conn->initCB(r, s, r);
//         return conn;
//     }
// };